{include /common/header}
<script>
    function form_submit(form){
        $.ajax({
            url: '{:\mylib\Route::buildUrl('/admin/content/add')}',
            type: 'POST',
            dataType: 'json',
            data: $(form).serialize(),
            success:function(res){
                alert(res.message);
                if (res.code) {
                    location.href = res.url;
                }
            },
            error:function(){
                alert('系统繁忙！');
            }
        });
        
        return false;
    }
</script>
<script>
    function file_upload(){
        var upload_id = document.getElementById('upload_id').value;
        var file = event.target.files[0];
        // 判断是否图片
        if(!file) return ;

        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(e){
            $.ajax({
                url: '{:\mylib\Route::buildUrl('/admin/api/upload')}',
                type: 'POST',
                dataType: 'json',
                data: {
                    name:file.name,
                    upload_id:upload_id,
                    file_data:reader.result
                },
                success:function(res){
                    if (res.code) {
                        $('#' + res.data.upload_id).val(res.data.filename);
                    }else{
                        alert(res.message);
                    }
                },
                error:function(){
                    alert('系统繁忙！');
                }
            });
        }
    }
    function upload_trigger(upload_id){
        document.getElementById('upload_form').reset();
        document.getElementById('upload_id').value = upload_id;
        document.getElementById('upload_input').click();
        return false;
    }
    function upload_add(id,field,style,dom){
        var rtime = (new Date()).getTime();
        var str = '<div class="input-group" style="margin-top:5px">';
            str += '<input type="text" id="upload_'+id+'_'+rtime+'" class="form-control" name="'+field+'[]" value="" style="'+style+'">';
            str += '<span class="input-group-btn">';
            str += ' <button class="btn btn-default" type="button" onclick="upload_del(this)">删除</button>';
            str += ' <button class="btn btn-default" type="button" onclick="upload_trigger(\'upload_'+id+'_'+rtime+'\')">上传</button>';
            str += '</span>';
            str += '</div>';
        $(dom).parent().before(str);
    }
    function upload_del(dom){
        $(dom).parent().parent().remove();
    }
</script>
<form id="upload_form" style="display: none;">
    <input type="hidden" id="upload_id" class="form-control" name="upload_id" value="">
    <input type="file" id="upload_input" onchange="file_upload()">
</form>
<ul class="nav nav-tabs" role="tablist">
    {php}$num=0;{/php}
    {foreach $groups as $group => $v}
        <li role="presentation" class="{if !$num}active{/if}"><a href="#{$group}" aria-controls="{$group}" role="tab" data-toggle="tab">{$group}</a></li>
        {php}$num+=1;{/php}
    {/foreach}
</ul>
<form action="{:\mylib\Route::buildUrl('/admin/content/add')}" onsubmit="return form_submit(this);" method="POST">
    <div class="tab-content" style="margin-top: 10px;">
        {php}$num=0;{/php}
        {foreach $groups as $group => $fields}
        <div role="tabpanel" class="tab-pane {if !$num}active{/if}" id="{$group}">
            {foreach $fields as $v}
            <div class="form-group">
            {switch $v['type']}

            {case 'text'}
            <label>{$v.title}</label>
            <input type="text" class="form-control" name="{$v.field}" style="{$v.style}" value="">
            {/case}

            {case 'textarea'}
            <label>{$v.title}</label>
            <textarea class="form-control" name="{$v.field}" rows="3" style="{$v.style}"></textarea>
            {/case}

            {case 'select'}
            <label>{$v.title}</label>
            <select class="form-control" name="{$v.field}" style="{$v.style}">
                {php}
                    $options = explode(PHP_EOL, $v['config']);
                {/php}
                {foreach $options as $vo}
                <option value="{$vo}">{$vo}</option>
                {/foreach}
            </select>
            {/case}

            {case 'checkbox'}
            <label>{$v.title}</label>
            {php}
                $options = explode(PHP_EOL, $v['config']);
            {/php}
            <div>
                {foreach $options as $vo}
                <label style="font-weight: normal;"><input type="checkbox" name="{$v.field}[]" value="{$vo}" style="{$v.style}"> {$vo}</label>
                {/foreach}
            </div>
            {/case}

            {case 'upload'}
            <label>{$v.title}</label>
            <div class="input-group">
                <input type="text" class="form-control" name="{$v.field}" value="" id="upload_{$v.id}" placeholder="" style="{$v.style}">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" onclick="upload_trigger('upload_{$v.id}')">上传</button>
                </span>
            </div>
            {/case}

            {case 'uploads'}
                <label>{$v.title}</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="{$v.field}[]" value="" id="upload_{$v.id}" placeholder="" style="{$v.style}">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" onclick="upload_trigger('upload_{$v.id}')">上传</button>
                    </span>
                </div>
                <div style="margin-top: 5px;">
                    <button class="btn btn-default" type="button" onclick="upload_add('{$v.id}','{$v.field}','{:str_replace(['\r\n', '\r', '\n'], '', $v['style'])}', this)">添加</button>
                </div>
            {/case}

            {case 'editor'}
            <label>{$v.title}</label>
            <textarea name="{$v.field}" rows="6" class="form-control xheditor {urlType:'abs',html5Upload:true,upMultiple:true,upLinkUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upLinkExt:'zip,rar,txt',upImgUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upImgExt:'jpg,jpeg,gif,png',upFlashUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upFlashExt:'swf',upMediaUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upMediaExt:'avi'}" style="{$v.style}"></textarea>
            {/case}

            {default}
            <label>未知类型:{$v.type}</label>
            {/default}

            {/switch}
            <p class="help-block">{$v.tips}</p>
            </div>
            {/foreach}
        </div>
        {php}$num+=1;{/php}
        {/foreach}
    </div>
    <input type="hidden" class="form-control" name="channel_id" value="{$channel_id}">
    <button type="submit" class="btn btn-primary">提交</button>
</form>
{include /common/footer}